package com.kkd.kkdapp.adapter;

import java.text.DecimalFormat;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.BidDetailsActivity;
import com.kkd.kkdapp.activity.MyDebtDetailActivity;
import com.kkd.kkdapp.activity.MyPlanDetailsActivity;
import com.kkd.kkdapp.http.entity.CalendarListDataItem;
import com.kkd.kkdapp.listener.OnSingleClickListener;

public class CalendarCurdayAdapter extends BaseAdapter {

	private Context mContext;
	private List<CalendarListDataItem> mList;
	private LayoutInflater inflater;
	private DecimalFormat df_point2;

	public CalendarCurdayAdapter(Context context,
			List<CalendarListDataItem> list) {
		mContext = context;
		mList = list;
		inflater = LayoutInflater.from(mContext);
		df_point2 = new DecimalFormat("###,##0.00");
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = inflater.inflate(
					R.layout.item_curday_calendar_listview, parent, false);
			holder = new ViewHolder();
			holder.head_title = convertView.findViewById(R.id.head_title);
			holder.tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
			holder.tv_status = (TextView) convertView
					.findViewById(R.id.tv_status);
			holder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
			holder.tv_num_title = (TextView) convertView
					.findViewById(R.id.tv_num_title);
			holder.tv_poidate = (TextView) convertView
					.findViewById(R.id.tv_poidate);
			holder.tv_poiterm = (TextView) convertView
					.findViewById(R.id.tv_poiterm);
			holder.tv_poiterm_company = (TextView) convertView
					.findViewById(R.id.tv_poiterm_company);
			holder.toDetail = convertView.findViewById(R.id.toDetail);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (position == 0) {
			holder.head_title.setVisibility(View.VISIBLE);
		} else {
			holder.head_title.setVisibility(View.GONE);
		}
		final CalendarListDataItem item = mList.get(position);
		if (item != null) {
			// 标题
			holder.tv_title.setText(item.getFinanceTitle());
			// 状态
			final String billStatusDesc = item.getBillStatusDesc();
			holder.tv_status.setText(billStatusDesc);
			if ("招标中".equals(billStatusDesc)) {
				holder.tv_status.setBackgroundResource(R.drawable.bidding_bg);
			} else if ("已满标".equals(billStatusDesc)) {
				holder.tv_status.setBackgroundResource(R.drawable.bidding_bg);
			} else if ("还款中".equals(billStatusDesc)) {
				holder.tv_status
						.setBackgroundResource(R.drawable.repaymentin_bg);
			} else if ("已还清".equals(billStatusDesc)) {
				holder.tv_status
						.setBackgroundResource(R.drawable.haspaidoff_bg);
			} else {
				holder.tv_status
						.setBackgroundResource(R.drawable.repaymentin_bg);
			}
			// 金额
			// 本金
			long principal = item.getExpectedPrincipal();
			// 利息
			long interest = item.getExpectedInterest()
					+ item.getExpectedExtraInterest()
					+ item.getExpectedCouponInterest();
			if (principal > 0 && interest == 0) {
				holder.tv_num_title.setText("本金还款(元)");
			} else if (principal == 0 && interest > 0) {
				holder.tv_num_title.setText("利息还款(元)");
			} else if (principal > 0 && interest > 0) {
				holder.tv_num_title.setText("本、息还款(元)");
			}
			holder.tv_num.setText(df_point2
					.format((principal + interest) * 0.01));
			// 投资日期
			holder.tv_poidate.setText(item.getApplyTimeDesc());
			// 投资期限
			String termDesc = item.getTermDesc();
			if (!TextUtils.isEmpty(termDesc)) {
				if (termDesc.contains("个")) {
					holder.tv_poiterm.setText(termDesc.substring(0,
							termDesc.indexOf("个")));
					holder.tv_poiterm_company.setText(termDesc
							.substring(termDesc.indexOf("个")));
				} else if (termDesc.contains("天")) {
					holder.tv_poiterm.setText(termDesc.substring(0,
							termDesc.indexOf("天")));
					holder.tv_poiterm_company.setText(termDesc
							.substring(termDesc.indexOf("天")));
				} else {
					holder.tv_poiterm.setText(termDesc);
					holder.tv_poiterm_company.setText("");
				}
			} else {
				holder.tv_poiterm.setText("");
				holder.tv_poiterm_company.setText("");

			}
			// 跳转到投资详情页
			holder.toDetail.setOnClickListener(new OnSingleClickListener() {

				@Override
				public void onSingleClick(View v) {
					String orderId = item.getOrderId();
					Intent intent;
					if (item.getIsFinance() == 1) { // K计划
						intent = new Intent(mContext,
								MyPlanDetailsActivity.class);
						intent.putExtra("poiId", orderId);
					} else if (item.getIsVirtualBid() == 1) {// 体验标
						intent = new Intent(mContext, BidDetailsActivity.class);
						intent.putExtra("poiId", orderId);
						intent.putExtra("isTiyan", true);
					} else {
						intent = new Intent(mContext,
								MyDebtDetailActivity.class);
						intent.putExtra("poi_id", orderId);
						intent.putExtra("type",
								"已还清".equals(billStatusDesc) ? 4 : 1);
					}
					mContext.startActivity(intent);
				}
			});
		}
		return convertView;
	}

	class ViewHolder {
		View head_title;
		TextView tv_title;
		TextView tv_status;
		TextView tv_num;
		TextView tv_num_title;
		TextView tv_poidate;
		TextView tv_poiterm;
		TextView tv_poiterm_company;
		View toDetail;
	}
}
